// pagesProperty/pages/oa/attendance/band/index.js
var app = getApp(), fetchApi = require("../../../../../utils/httpPost.js")
, common_Js = require("../../../../../component/common.js");
import Dialog from '../../../../../component/vant/dialog/dialog';
import Toast from '../../../../../component/vant/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    submitState: false,
    attendanceUsers:[],
    // attendanceUsers:[],
    phone_num:'',
    bandUserType:0,
    bandSysUserId:0,
    chooseBandUserId:0,
    groupUserId:0,
    isBand:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  bindInput(e){
    console.log('bindInput',e);
    this.data.phone_num = e.detail;
  },
  goSearchUser(e){
    if(this.data.phone_num == ''){
      return Toast('请输入手机号');
    }
    this.onHttpSearchUser();
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpSearchUser: function () {
    var that = this;
    fetchApi.httpPost('/attendance/group/user/list', { 
      phone_num: that.data.phone_num,
    },function (res) {
        console.log('获取手机号绑定的用户列表', res)
        if (res.data.code == 0) {
          that.setData({
            attendanceUsers: res.data.data.attendanceUsers,
          })
        }
        that.setData({
          isLoading:false
        })
    },function(){})
  },
  chooseEstateUser(e){
    console.log('chooseEstateUser',e);
    var indexClick = e.currentTarget.dataset.index;
    var item = e.currentTarget.dataset.item;

    this.data.groupUserId = item.id;
    for(var index in this.data.attendanceUsers){
      this.data.attendanceUsers[index].isCheck = 0;
    }

    this.data.chooseBandUserId = item.id;
    for(var index in this.data.attendanceUsers){
      if(indexClick == index){
        this.data.attendanceUsers[index].isCheck = 1;
      }
    }

    this.data.bandSysUserId = item.sysUserId;
    this.data.bandUserType = 3;
    console.log('bandSysUserId', item.sysUserId);
    this.setData({
      isBand: item.isBand,
      bandSysUserId: item.sysUserId,
      attendanceUsers: this.data.attendanceUsers
    })
  },
  choosePropertyUser(e){
    console.log('choosePropertyUser',e);
    var indexClick = e.currentTarget.dataset.index;
    var item = e.currentTarget.dataset.item;

    this.data.groupUserId = item.id;
    for(var index in this.data.attendanceUsers){
      this.data.attendanceUsers[index].isCheck = 0;
    }

    this.data.chooseBandUserId = item.id;
    this.data.bandUserType = 2;
    for(var index in this.data.attendanceUsers){
      if(indexClick == index){
        this.data.attendanceUsers[index].isCheck = 1;
      }
    }

    this.data.bandSysUserId = item.sysUserId;
    console.log('bandSysUserId', item.sysUserId);
    this.setData({
      bandSysUserId: item.sysUserId,
      attendanceUsers: this.data.attendanceUsers
    })
  },
  /**
   * 绑定操作操作
   */
  bandSubmitOp:function(){
    var that = this;
    if(this.data.bandSysUserId == undefined || this.data.bandSysUserId == 0){
      Toast('请选择要操作的用户');
      return;
    }
    if(that.data.submitState){
        Toast.fail('正在提交...');
        return;
    }
    that.data.submitState = true;
    that.setData({submitState : true })
    fetchApi.httpPost('/attendance/band/group/user', {
      user_type: that.data.bandUserType,
      sys_user_id: that.data.bandSysUserId,
      group_user_id: that.data.groupUserId,
    }, function (res) {
      console.log("绑定人员操作", res)
      if (res.data.code == 0) {
        that.data.submitState = false;
        that.setData({
            submitState : false,
            // attendanceUsers:[],
        })
        Toast.success(res.data.msg);
        that.onHttpSearchUser();
      }else{
        that.data.submitState = false;
        that.setData({
            submitState : false,
        })
        Toast.fail( res.data.msg);
        return;
      }
    },function(){})
  },
  //解绑提示
  goUnBandUser(e){
    console.log('goUnBandUser e',e)
    var that = this;
    var message = "是否要解绑该用户？"
    Dialog.confirm({
      title: '解绑用户',
      message
    }).then(() => {
      that.unBandSubmitOp()
    })
    .catch(() => {
      Dialog.close();
    });
  },
  /**
   * 解绑操作
   */
  unBandSubmitOp:function(){
    var that = this;
    if(this.data.bandSysUserId == 0){
      Toast('请选择要操作的用户');
      return;
    }
    if(that.data.submitState){
        Toast.fail('正在提交...');
        return;
    }
    that.data.submitState = true;
    that.setData({submitState : true })
    fetchApi.httpPost('/attendance/unBand/group/user', {
      user_type: that.data.bandUserType,
      sys_user_id: that.data.bandSysUserId,
      group_user_id: that.data.groupUserId,
    }, function (res) {
      console.log("解绑人员操作", res)
      if (res.data.code == 0) {
        that.data.submitState = false;
        that.setData({
          submitState : false,
          // attendanceUsers:[],
        })
        that.onHttpSearchUser();
        Toast.success(res.data.msg);
      }else{
        that.data.submitState = false;
        that.setData({
            submitState : false,
        })
        Toast.fail( res.data.msg);
        return;
      }
    },function(){})
  },
})